/**
 * 
 */
package com.tc.dispatch.web.schedule.cost;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.csservice.ejb.CSServiceLocal;
import com.tc.dispatch.gsservice.basic.common.tcdate;
import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.web.csservice.csHelper;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.model.combinedDensity;
import com.tc.dispatch.web.util.DateJsonValueProcessor;
import com.tc.dispatch.web.util.tools;

/**
 * @author cajeep
 * 
 */
public class priceInf extends HttpServlet {


	/**
	 * Constructor of the object.
	 */
	public priceInf() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}



	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		// response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		CSServiceLocal css  = csHelper.getCSServiceLocal();
		GSServiceLocal gss=gsHelper.getGSServiceLocal() ;
		HttpSession sess = request.getSession();
		if (action.equals("list")) {
			String page = request.getParameter("start");
			if (page == null)
				page = "0";
			String pagePerNum = request.getParameter("limit");
			if (pagePerNum == null)
				pagePerNum = "10";
			try {

				List ret = css.findMinManuCostByPage(Integer.parseInt(page), Integer
						.parseInt(pagePerNum)," techfd=1111 and  reqdate >'2008-01-01'");
				System.out.println("ret.size():"+ret.size());
				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());

				// BeanUtils.copyProperties();
				json.put("totalProperty", new Integer(css
					.getMinManuCostTotalNums(" (techfd=1111 and  reqdate >'2008-01-01' )")));

				JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
				json.put("root", arr);

				response.getWriter().print(json);
			} catch (Exception e) {

				System.out.println("Exception is found in list :" + e.getMessage());
				response.getWriter().print("{success:false}");
			}

		} else if (action.equals("all")) {
			String page = request.getParameter("start");
			if (page == null)
				page = "0";
			String pagePerNum = request.getParameter("limit");
			if (pagePerNum == null)
				pagePerNum = "10";
			String dateFrom=request.getParameter("dateFrom");
			if(dateFrom==null)
				dateFrom=(String)sess.getAttribute("dateFrom");
			String dateTo=request.getParameter("dateTo");
			if(dateTo==null)
				dateTo=(String)sess.getAttribute("dateTo");
			String transnum=request.getParameter("transnum");
			
			try {
				String whereStr="";
				if(transnum!=null&&!transnum.equals(""))
				{
					whereStr=" ( transnum='"+transnum+"' and reqdate between '"+dateFrom+"' and '"+dateTo+"')";
					
				}
				else
					whereStr=" ( reqdate between '"+dateFrom+"' and '"+dateTo+"')";
					
				List ret = css.findMinManuCostByPage(Integer.parseInt(page), Integer
						.parseInt(pagePerNum),whereStr);
				sess.setAttribute("dateFrom", dateFrom);
				sess.setAttribute("dateTo", dateTo);
				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());

				// BeanUtils.copyProperties();
				json.put("totalProperty", new Integer(css
						.getMinManuCostTotalNums(whereStr)));

				JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
				json.put("root", arr);

				response.getWriter().print(json);
			} catch (Exception e) {

				System.out.println("list error:" + e.getMessage());
				response.getWriter().print("{success:false}");
			}

		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here

	}

}
